// pages/mine/editBirthday/editBirthday.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    now: "",
    birthday: "",
    oldValue: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      now: new Date().format("yyyy-MM-dd"),
      birthday: options.birthday,
      oldValue: options.birthday
    })
  },

  bindPickerChange(e) {
    var key = e.target.dataset.key;

    this.setData({
      [key]: e.detail.value
    })

  },
  submit() {
    let that = this;
    if (that.data.birthday == that.data.oldValue) return;
    wx.getStorage({
      key: 'parent',
      success: function(res) {
        let stdtPantId = res.data.stdtPantId;
        app.api.ajaxSubmit("/node/wx/mine/birthday/edit", {
          birthday: that.data.birthday,
          stdtPantId: stdtPantId
        }).then(result => {
          if (result.data.error == 10000) {
            res.data.birthday = that.data.birthday;
            wx.setStorageSync("parent", res.data)
            wx.showToast({
              title: '设置成功',
              duration: 2000,
              icon: "none"
            })
            that.updateInfo();
            that.cancel();
          }
        }).catch(err => {
          wx.showToast({
            title: '服务器开小差',
            duration: 2000,
            icon: "none"
          })
        })
      },
    })

  },
  cancel() {
    wx.navigateBack({
      delta: 1
    })
  },
  //更新前面个页面的生日信息
  updateInfo() {
    let that = this;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //上一个页面
    let minePage = pages[pages.length - 3]; //我的页面
    prevPage.setData({
      birthday: that.data.birthday
    })

    minePage.setData({
      birthday: that.data.birthday
    })
  }
})